﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Smartec.Accounts.Camel.AMS.Model.Entities;
using Telerik.Web.Mvc;
using Smartec.Accounts.Camel.AMS.Model.Facades;

namespace Smartec.Accounts.Camel.AMS.WebApp.Controllers
{

    public class SystemAuditController : Controller
    {
        //
        // GET: /Adresses/
        [CustomAuthorize(Action = "View")]
        public ActionResult Index(DateTime? dateFrom, DateTime? dateUntil, string entity, string entityId, string userName, string dbAction)
        {
            ViewBag.dateFrom = dateFrom;
            ViewBag.dateUntil = dateUntil;
            ViewBag.tableName = entity;
            ViewBag.entityId = entityId;
            ViewBag.userName = userName;
            ViewBag.dbAction = dbAction;

            List<DBAuditDetail> dbAudits = new List<DBAuditDetail>();
            if (AMSManager.InsideIframe)
            {
                return View("Index", "Partial", dbAudits);
            }
            else
            {
                return View(dbAudits);
            }
        }

        [GridAction]
        public ActionResult _index(DateTime? dateFrom, DateTime? dateUntil, string tableName, string entityId, string userName, string dbAction)
        {
            int _entityId = -1;

            bool parsed = Int32.TryParse(entityId, out _entityId);

            IEnumerable<DBAuditDetail> dbAudits = DBAuditFacade.FindAllDBAuditDetails(dateFrom, dateUntil, tableName, parsed ? _entityId : -1, userName, dbAction);

            return View(new GridModel<DBAuditDetail>
            {
                Data = dbAudits
            });
        }

    }
}
